using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using Sinacor.Infra.Common.Serialization;
using System.Windows.Input;

namespace Sinacor.Infra.UI.Controls.SmartClient
{
    [TemplatePart(Name = "PART_ButtonClose", Type = typeof(ButtonBase))]
    public class SinacorTabItemBrowser : SinacorTabItem
    {
        #region Constructor

        public SinacorTabItemBrowser() : base()
        {
            CommandBindings.Add(new CommandBinding(SinacorTabControlCommands.Close, closeTabItemClick));
        }

        #endregion

        #region Dependancy Properties

        /// <summary>
        /// Allow the SinacorTabItemBrowser to be Deleted by the end user
        /// </summary>
        public bool AllowDelete
        {
            get { return (bool)GetValue(AllowDeleteProperty); }
            set { SetValue(AllowDeleteProperty, value); }
        }
        
        public static readonly DependencyProperty AllowDeleteProperty = DependencyProperty.Register("AllowDelete", typeof(bool), typeof(SinacorTabItemBrowser), new UIPropertyMetadata(true));

        #endregion

        #region Methods

        /// <summary>
        /// OnMouseEnter, Create and Display a Tooltip if the Header is cropped
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseEnter(System.Windows.Input.MouseEventArgs e)
        {
            base.OnMouseEnter(e);

            XamlSerializer xaml = new XamlSerializer();

            this.ToolTip = xaml.Deserialize(xaml.Serialize(Header));

            e.Handled = true;
        }

        /// <summary>
        /// OnMouseLeave, remove the tooltip
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeave(System.Windows.Input.MouseEventArgs e)
        {
            base.OnMouseLeave(e);

            this.ToolTip = null;
            e.Handled = true;
        }

        /// <summary>
        /// Close current SinacorTabControlBrowser
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void closeTabItemClick(object sender, ExecutedRoutedEventArgs e)
        {
            // get the parent tabcontrol 
            SinacorTabControlBrowser tc = (SinacorTabControlBrowser)ControlHelper.FindParent(this, typeof(SinacorTabControlBrowser));
            if (tc == null) return;

            // remove this SinacorTabItemBrowser from the parent tabcontrol
            tc.RemoveItem(this);
        }

        #endregion
    }
}
